package com.southwind.service.impl;

import com.southwind.dao.DormitoryAdminDao;
import com.southwind.dao.impl.DormitoryAdminDaoImpl;
import com.southwind.dto.DormitoryAdminDto;
import com.southwind.dto.SystemAdminDto;
import com.southwind.entity.DormitoryAdmin;
import com.southwind.service.DomritoryAdminService;

import java.util.List;

public class DomritoryAdminServiceImpl implements DomritoryAdminService {
    DormitoryAdminDao dormitoryAdminDao = new DormitoryAdminDaoImpl();
    @Override
    public List<DormitoryAdmin> list() {
        return this.dormitoryAdminDao.list();
    }

    @Override
    public List<DormitoryAdmin> search(String key, String value) {
        if(value.equals("")) {return this.dormitoryAdminDao.list();};

        return this.dormitoryAdminDao.search(key,value);
    }

    @Override
    public void save(DormitoryAdmin dormitoryAdmin) {
        Integer save = dormitoryAdminDao.save(dormitoryAdmin);
        if(save != 1) throw new RuntimeException("宿管信息添加失败");
    }

    @Override
    public void update(DormitoryAdmin dormitoryAdmin) {
        Integer update = dormitoryAdminDao.update(dormitoryAdmin);
        if(update != 1) throw new RuntimeException("宿管信息更新失败");
    }

    @Override
    public void delete(Integer id) {
        Integer update = dormitoryAdminDao.delete(id);
        if(update != 1) throw new RuntimeException("宿管信息删除失败");
    }

    @Override
    public DormitoryAdminDto login(String username, String password) {
        //查询数据库
        DormitoryAdmin dormitoryAdmin = dormitoryAdminDao.findByUsername(username);
        DormitoryAdminDto dormitoryAdminDto = new DormitoryAdminDto();
        //判断
        if(dormitoryAdmin == null)
        {
            dormitoryAdminDto.setCode(-1);
            dormitoryAdminDto.setDormitoryAdmin(dormitoryAdmin);
            return dormitoryAdminDto;
        }else {
            if((!password.equals(dormitoryAdmin.getPassword())))
            {
                dormitoryAdminDto.setCode(-2);
                dormitoryAdminDto.setDormitoryAdmin(dormitoryAdmin);
                return dormitoryAdminDto;
            }else
            {
                dormitoryAdminDto.setCode(0);
                dormitoryAdminDto.setDormitoryAdmin(dormitoryAdmin);
            }

        }
        //资源释放
        return dormitoryAdminDto;
    }
}
